package com.ucsc.mit.system.login;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.dto.convertors.UserAuthenticatorConvertor;
import com.office.automate.encrypt.password.EncryptPassword;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.authenticate.ChangePassword;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.service.def.authenticate.SystemLoginAuthenticationService;
import com.office.automate.service.impl.authenticate.SystemLoginAuthenticationServiceImplementer;
import com.office.automate.validation.framework.ValidatorObject;

/**
 * Servlet implementation class ChangePasswordServlet
 */
@WebServlet("/ChangePasswordServlet")
public class ChangePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/HomePage.jsp");
		try {
			SystemLoginAuthenticationService systemLoginAuthenticationService = new SystemLoginAuthenticationServiceImplementer();
			UserAuthenticator currentUser = (UserAuthenticator) (request.getSession().getAttribute("currentSessionUser"));
			ChangePassword changePassword = new UserAuthenticatorConvertor().convertPasswordChangeRequest(request);
			changePassword.setUsername(currentUser.getUsername());
			changePassword.setEmployeeId(currentUser.getEmployeeId());
			HashMap<String, FrontierVisibleException> fieldValidationErrors = 
												systemLoginAuthenticationService.validateChangePasswordDetails(changePassword);
			if (fieldValidationErrors != null) {
				request.setAttribute("ChangePassword", changePassword);
				request.setAttribute("FieldValidationErrors", new ValidatorObject(fieldValidationErrors));
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ChangePassword.jsp");
			} else {
				changePassword.setNewPassword(new EncryptPassword().getEncryptedPasswordMD5(changePassword.getNewPassword()));
				systemLoginAuthenticationService.changePassword(changePassword);
			}
			dispatcher.forward(request, response);				
		} catch (FrontierVisibleException e) {
			request.setAttribute("OperationalError", e);
			dispatcher.forward(request, response);
		}
	}
}
